package com.woniu.elderly.wares.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.dto.WaresDTO;
import com.woniu.common.entity.Result;
import com.woniu.elderly.wares.service.WaresSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: xuan
 * @Date: 2024/8/3 11:58
 * @Version: 1.0
 */

@RestController
@RequestMapping("/waresSku")
@Slf4j
public class WaresSkuController {

    @Autowired
    private WaresSkuService waresSkuService;
    @GetMapping("/")
    public Result<List<WaresDTO>> getWaresSku(){
        // 调用waresSkuService的getWaresSku方法获取商品SKU列表
        List<WaresDTO> waresSku = waresSkuService.getWaresSkuAll();
        // 将获取到的商品SKU列表封装为Result对象并返回
        return Result.ok(waresSku);
    }
    @GetMapping("/{pageIndex}/{pageSize}")
    public Result<Page<WaresDTO>> getWaresSkuPage(
            @PathVariable("pageIndex") Integer pageIndex,
            @PathVariable("pageSize") Integer pageSize,
            HttpServletRequest request
    ){

        log.info("pageIndex:{}, pageSize:{}", pageIndex, pageSize);
        //记录uv
        waresSkuService.recordUV(request);
        // 调用waresSkuService的getWaresSku方法获取商品SKU列表
        Page<WaresDTO> waresSku= waresSkuService.getWaresSkuPage(pageIndex, pageSize);
        // 将获取到的商品SKU列表封装为Result对象并返回
        return Result.ok(waresSku);
    }
    //随机推荐商品，展示在首页
    @GetMapping("/random")
    public Result<List<WaresDTO>> getRecommend(){
        //得到推荐商品列表
       return waresSkuService.getRecommend();
    }
    //添加商品,没写完
    @PostMapping("/")
    public Result<Void> addWaresSku(@RequestBody WaresDTO waresSku){
        waresSkuService.addWaresSku(waresSku);
        return Result.ok();
    }
    //根据传来的skuid查询商品
    @GetMapping("/{skuId}")
    public Result<WaresDTO> getWaresSku(@PathVariable("skuId") Integer skuId) {
        // 调用waresSkuService的getWaresSku方法获取商品SKU列表
        WaresDTO waresSku = waresSkuService.getWaresSkuBySkuSid(skuId);
        // 将获取到的商品SKU列表封装为Result对象并返回
        return Result.ok(waresSku);
    }
    //下架具体商品sku
    @PutMapping("/down/{skuId}")
    public Result<Void> updateWaresSkuDown(@PathVariable("skuId") Integer skuId){
        waresSkuService.updateWaresSku(skuId,0);
        log.info("下架");
        return Result.ok("下架成功");
    }
    @PutMapping("/up/{skuId}")
    public Result<Void> updateWaresSkuUp(@PathVariable("skuId") Integer skuId){
        waresSkuService.updateWaresSku(skuId,1);
        log.info("上架");
        return Result.ok("上架成功");
    }
    @PutMapping("/stock/{skuId}/{num}")
    public Result<Void> updateWaresSkuStock(@PathVariable("skuId") Integer skuId,@PathVariable("num")Integer num){
        log.info("减库存：skuId:{},num:{}",skuId,num);
        waresSkuService.stock(skuId,num);
        return Result.ok("库存减少成功");
    }
}
